package com.hd.crdao.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageButton;

import com.hd.crdao.R;
import com.hd.crdao.activity.SearchReportActivity;
import com.hd.crdao.adapter.CarSourcePagerAdapter;
import com.hd.crdao.base.BaseFragment;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *
 *@author Administrator
 *@date 2017/6/26 0026
 */
public class ReportFrangment extends BaseFragment {
    @BindView(R.id.pagersligingtabstrip)
    PagerSlidingTabStrip mPagersligingtabstrip;
    @BindView(R.id.pager)
    ViewPager mPager;
    @BindView(R.id.back)
    ImageButton mBack;
    @BindView(R.id.search)
    ImageButton mSearch;
    private List<Fragment> mFragments = new ArrayList<>();
    public DisplayMetrics mDisplayMetrics;
    public CarSourcePagerAdapter mCarSourcePagerAdapter;

    private final String[] titles = {"全部", "查询成功", "查询失败"};


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_report;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this, mViewRoot);

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mDisplayMetrics = getResources().getDisplayMetrics();
        for (int i = 0; i < titles.length; i++) {
            mFragments.add(i,getFragment(titles[i]));
        }
        mCarSourcePagerAdapter = new CarSourcePagerAdapter(getActivity().getSupportFragmentManager(), mFragments, titles);
        mPager.setAdapter(mCarSourcePagerAdapter);
        mPagersligingtabstrip.setViewPager(mPager);
        setTabValue();
    }

    /**
     * 放入一个标记,加载不同的数据,以及界面显示
     * @param value
     * @return
     */
    private Fragment getFragment(String value) {
        MyReportFragment myReportFragment = new MyReportFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", value);
        myReportFragment.setArguments(bundle);
        return myReportFragment;
    }


    /**
     * 设置tab的一些参数
     */
    private void setTabValue() {
        // 设置Tab是自动填充满屏幕的
        mPagersligingtabstrip.setShouldExpand(true);

        // 设置Tab的分割线是透明的
        mPagersligingtabstrip.setDividerColor(Color.TRANSPARENT);
        // 设置Tab底部线的高度
        mPagersligingtabstrip.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, mDisplayMetrics));
        // 设置Tab Indicator的高度
        mPagersligingtabstrip.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 2, mDisplayMetrics));
        // 设置Tab标题文字的大小
        mPagersligingtabstrip.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, mDisplayMetrics));
        // 设置Tab标题默认的颜色*
        mPagersligingtabstrip.setTextColor(getResources().getColor(
                R.color.get_record_text_unselected_color));
        // 设置选中Tab标题的颜色
        mPagersligingtabstrip.setSelectedTextColor(getResources().getColor(
                R.color.get_record_text_selected_color));
        // 设置Tab底部线的颜色
        mPagersligingtabstrip.setUnderlineColor(getResources().getColor(
                R.color.get_record_line_unselected_color));
        // 设置Tab Indicator的颜色
        mPagersligingtabstrip.setIndicatorColor(getResources().getColor(
                R.color.get_record_line_selected_color));
        // 取消点击Tab时的背景色
        // mPagersligingtabstrip.setTabBackground(getResources().getColor(R.color.tab_pressed_hover));
    }


    @OnClick({R.id.back, R.id.search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                getActivity().finish();
                break;
            case R.id.search:
                startActivity(new Intent(UIUtils.getContext(), SearchReportActivity.class));
                break;
        }
    }
}
